﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        ListItem[] _fuJianCityList = new ListItem[] 
        {
            new ListItem("龙岩"),
            new ListItem("三明"),
            new ListItem("福州"),
        };
        ListItem[] _guangDongCityList = new ListItem[]
        {
            new ListItem("深圳"),
            new ListItem("珠海"),
            new ListItem("广州"),
        };
        ListItem[] _guangXiCityList = new ListItem[]
        {
            new ListItem("柳州"),
            new ListItem("桂林"),
            new ListItem("南宁"),
        };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlProvince.Items.AddRange(new ListItem[] 
                {
                    new ListItem("福建"),
                    new ListItem("广东"),
                    new ListItem("广西"),
                });
                ddlCity.Items.AddRange(_fuJianCityList);
                
            }
            img.Visible = false;
            ltrImg.Visible = false;
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCity.Items.Clear();
            switch (ddlProvince.SelectedValue)
            {
                case "福建":
                    ddlCity.Items.AddRange(_fuJianCityList);
                    break;
                case "广东":
                    ddlCity.Items.AddRange(_guangDongCityList);
                    break;
                case "广西":
                    ddlCity.Items.AddRange(_guangXiCityList);
                    break;
                default:
                    break;
            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ltrInfo.Text = "";

            if (tbxUserName.Text == "" || tbxPassword.Text == "")
            {
                Response.Write("<script>alert('用户名或密码不能为空')</script>");
                return;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append($"用户名：{tbxUserName.Text}<br/>");
            sb.Append($"性别：{rblGender.SelectedValue}<br/>");
            sb.Append($"爱好：{tbxHobby.Text}<br/>");
            sb.Append($"省：{ddlProvince.SelectedValue}<br/>");
            sb.Append($"市：{ddlCity.SelectedValue}<br/>");
            sb.Append($"其他信息：{tbxInfo.Text}<br/>");
            ltrInfo.Text = sb.ToString();
            if (fulImage.HasFile)
            {
                string fileName = fulImage.FileName;
                if (fulImage.FileName.EndsWith("jpg") || fulImage.FileName.EndsWith("png") || fulImage.FileName.EndsWith("jpeg"))
                {
                    Directory.CreateDirectory(Server.MapPath("~/") + "img");
                    string url = Server.MapPath("/img/" + fileName);
                    fulImage.SaveAs(url);
                    img.ImageUrl = "/img/" + fileName;
                    ltrImg.Visible = true;
                    img.Visible = true;
                }
            }
        }
    }
}